package action;



import java.util.ArrayList;

import java.util.Map;


import models.DeveloperData;
import models.IssueData;
import models.TagData;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String commandButton = "";
	private String selectedTagId;
	public String getSelectedTagId() {
		return selectedTagId;
	}

	public void setSelectedTagId(String selectedTagId) {
		this.selectedTagId = selectedTagId;
	}

	Map<String, Object> session;
	ArrayList<IssueData> IssueList= new ArrayList<IssueData>();
	ArrayList<TagData> TagList=new ArrayList<TagData>();
	public ArrayList<TagData> getTagList() {
		return TagList;
	}

	public void setTagList(ArrayList<TagData> tagList) {
		TagList = tagList;
	}

	ArrayList<String> TagNamesList=new ArrayList<String>();
	public ArrayList<String> getTagNamesList() {
		return TagNamesList;
	}

	public void setTagNamesList(ArrayList<String> tagNamesList) {
		TagNamesList = tagNamesList;
	}

	public String getCommandButton() {
		return commandButton;
	}

	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}

	private String username="";
    private String password="";
    
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }

	public String execute() {
      //  MyLogAction.log("in Login.execute() with commandButton = " + 
	   //     this.commandButton);
      System.out.println("in Login.execute() with commandButton = " + 
    	       this.commandButton);
		if(this.commandButton.equalsIgnoreCase("Developer Register")){
			System.out.println("in dev reg");
			return "dev_reg";
		}
		
		if(this.commandButton.startsWith("Lodge")){
			System.out.println("in lodge");
			return "issue_lodge";
		}
			
		
		
        if (this.username.isEmpty()) {
			// first time screen
        	addActionError(getText("User Id can not be Empty !!"));
        	System.out.println("empty");
		    return "initial_entry";
        } 
        if (this.password.isEmpty()) {
			// first time screen
        	addActionError(getText("Password can not be Empty !!"));
        	System.out.println("empty");
		    return "initial_entry";
        } 
        DeveloperData Developer = DeveloperData.findOne("where dev_uname = '"+ username +"' and dev_pass = '"+ password +"'");
        
        if (this.username.equals("admin") 
                && this.password.equals("admin123")||Developer!=null) {
        	TagList=TagData.tag_name_unique("");
        	for(int i=0;i<TagList.size();i++){
        		TagNamesList.add(TagList.get(i).getTagName());
        		//session.put("tag","tag");
        		//System.out.println("--------------------" + TagNamesList.get(i) + "------------------------------");
        	}
        	
        	session = ActionContext.getContext().getSession();
        	session.put("Developer",Developer);
        	
        	
        	if(Developer!=null){
        		session.put("loggedin","Developer");
        	}
        	else{
            	session.put("loggedin","admin");

        	}
        	IssueList = IssueData.find("");
        	System.out.println("inlogacc  "+IssueList.get(0).getAssignedto());
        	session.put("TagList", TagList );
        	session.put("IssueList",IssueList);
            return "success";
        } 

        addActionError(getText("Incorrect user id and / or password!"));
        return "error";
    }

	public ArrayList<IssueData> getIssueList() {
		return IssueList;
	}

	public void setIssueList(ArrayList<IssueData> issueList) {
		IssueList = issueList;
	}

	
    
}
